package com.heihei.authority.browser;


import com.heihei.authority.browser.authentication.FeiAuthenctiationFailureHandler;
import com.heihei.authority.browser.authentication.FeiAuthenticationSuccessHandler;
import com.heihei.authority.core.authentication.AbstractChannelSecurityConfig;
import com.heihei.authority.core.authentication.mobile.SmsCodeAuthenticationSecurityConfig;
import com.heihei.authority.core.authentication.mobile.SmsCodeFilter;
import com.heihei.authority.core.properties.FeiSecurityConstants;
import com.heihei.authority.core.properties.MySecurityProperties;
import com.heihei.authority.core.validate.code.ValidateCodeFilter;
import com.heihei.authority.core.validate.code.ValidateCodeSecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.springframework.social.security.SpringSocialConfigurer;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 *  浏览器安全配置
 *
 * @version 1.0
 * @since JDK1.8
 * @author duanhengfei
 * @date 2021年03月28日 16:50:56
 */
@Configuration
public class BrowserSecurityConfig extends AbstractChannelSecurityConfig {
	@Bean
	public PasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}

	@Bean
	public PersistentTokenRepository persistentTokenRepository() {
		JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
		tokenRepository.setDataSource(dataSource);
		//初始化时，创建表结构
//		tokenRepository.setCreateTableOnStartup(true);
		return tokenRepository;
	}

	@Resource
	private UserDetailsService userDetailsService;

	@Resource
	private ValidateCodeSecurityConfig validateCodeSecurityConfig;

	@Resource
	private DataSource dataSource;

	@Autowired
	private MySecurityProperties securityProperties;

	@Resource
	private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;

	@Resource
	private SpringSocialConfigurer heiheiSocialSecurityConfig;
	
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		applyPasswordAuthenticationConfig(http);

		http.apply(validateCodeSecurityConfig)
				.and()
			.apply(smsCodeAuthenticationSecurityConfig)
				.and()
			.apply(heiheiSocialSecurityConfig)
				.and()
			.rememberMe()
				.tokenRepository(persistentTokenRepository())
				.tokenValiditySeconds(securityProperties.getBrowser().getRememberMeSeconds())
				.userDetailsService(userDetailsService)
				.and()
			.authorizeRequests()
				.antMatchers(
						FeiSecurityConstants.DEFAULT_UNAUTHENTICATION_URL,
						FeiSecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_MOBILE,
						securityProperties.getBrowser().getLoginPage(),
						FeiSecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX + "/*")
						.permitAll()
				.anyRequest()
				.authenticated()
				.and()
			.csrf().disable();
	}
	
}
